#!/usr/bin/env python3
#coding:utf-8
import time
import getopt
import sys
from selenium import webdriver

chrome_driver_path = '/Users/zhangyuan/chromedriver2'

def get_baidu_attribution(s):
	# print(s)
	options = webdriver.ChromeOptions()
	# options.add_argument('headless')
	driver = webdriver.Chrome(executable_path=chrome_driver_path,options=options)
	driver.get('http://www.baidu.com/');
	search_box = driver.find_element_by_id('kw')
	search_box.send_keys(s)
	search_box.submit()
	time.sleep(1)
	all_options = driver.find_elements_by_class_name("op_mobilephone_r")
	all_options_class = driver.find_elements_by_xpath('//*[@id="content_left"]//*[contains(@class, "cr-title")]')
	all_options_tag = driver.find_elements_by_xpath('//*[@id="1"]/div/div[2]/div[1]/div/div[2]/div[1]/span[2]')
	
	if len(all_options) > 0:
		print(s,all_options[0].text[19:])
	if len(all_options_class) > 0:
		print(s,all_options_class[0].text)
	if len(all_options_tag) > 0:
		print(s,all_options_tag[0].text,"缺少运营商")
	driver.quit()
    
def get_sogou_attribution(s):
	options = webdriver.ChromeOptions()
	# options.add_argument('headless')
	driver = webdriver.Chrome(executable_path=chrome_driver_path,options=options)
	driver.get('http://www.sogou.com/')
	search_box = driver.find_element_by_id('query')
	search_box.send_keys(s)
	search_box.submit()
	time.sleep(1)
	h = driver.find_elements_by_id("phonenumberdetail")
	if len(h) > 0:
		print(s,h[0].text)
	else:
		print(s,"获取失败")
	driver.quit()

def help():
	print("\n使用方法1:\n\npython main.py --soure=[sogou|baidu] --start=15510000000 --end=15510200000")
	print("\n使用方法2:\n\npython main.py --soure=[sogou|baidu] --no=15510200000")

if __name__ == '__main__':
	try:
		opts, args = getopt.getopt(sys.argv[1:], "hp:i:", ["help", "start=", "end=","no=","soure="])
	except getopt.GetoptError:
		print("参数错误")
		help()
		sys.exit()

	no = ''
	start = ''
	end = ''
	use_soure = 'sougou'

	for option, value in opts:
		if option in ("--soure"):
			if value == "baidu":
				use_soure = 'baidu'
		if option in ("-s", "--start"):
			if len(value) != 11:
				sys.exit("start参数长度有误")
			start = value
		if option in ("-e", "--end"):
			if len(value) != 11:
				sys.exit("end参数长度有误")
			end = value
		if option in ("--no"):
			if len(value) != 11:
				sys.exit("no参数长度有误")
			no = value

	if len(no) == 11:
		print('获取'+no+'归属地')
		if 	use_soure == 'baidu':
		    get_baidu_attribution(no)
		else:
			get_sogou_attribution(no)
		sys.exit()

	if len(start) == 11 and len(end) == 11:
		if start[0:3] != end[0:3]:
			sys.exit("号段必须一致")
		if int(start[3:7]) > int(end[3:7]):
			sys.exit("开始号段必须大于结束号段")
		print("开始号段扫描 " + start[0:7] +"-"+ end[0:7])
		for num in range(int(start[3:7]),int(end[3:7])+1):
			if 	use_soure == 'baidu':
				c_str = str(num)
				if num < 1000:
					c_str = "0"+str(num)
				if num < 100:
					c_str = "00"+str(num)
				if num < 10:
					c_str = "000"+str(num)	
				search_str = start[0:3] + c_str + "0000"
				get_baidu_attribution(search_str)
			else:
				get_sogou_attribution(start[0:3] + str(num) + "0000")
		sys.exit()
	help()
